//
//  FlatToolBarItem.m
//  AppFrameWork
//
//  Created by Le Anh Tai on 7/19/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "FlatToolBarViewItem.h"
#import "UIConstants.h"
#import "UIImage+Color.h"

#define COLOR_SEPARATOR [UIColor blackColor]
#define COLOR_HIGHTLIGHTED [UIColor blackColor]
#define COLOR_NORMAL [UIColor whiteColor]
#define FONT_SIZE 18

@implementation FlatToolBarViewItem

+ (id)toolbarItemWithTitle:(NSString *)title icon:(NSString *)icon highlightHeight:(NSInteger)highlightHeight
{
    FlatToolBarViewItem *item = [FlatToolBarViewItem buttonWithType:UIButtonTypeCustom];
    [item setTitle:title forState:UIControlStateNormal];
    [item setTitleColor:COLOR_BLUE_IOS7 forState:UIControlStateHighlighted];
    [item setSelected:NO];
    item.highlightHeight = highlightHeight;
    
    if (icon && icon.length > 0)
    {
//        UIImage *selectedImage = [[UIImage imageNamed:icon] imageWithTint:COLOR_BLUE_IOS7];
        
        [item setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 4)];
        [item setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
//        [item setImage:[UIImage imageNamed:icon] forState:UIControlStateHighlighted];
//        [item setImage:[UIImage imageNamed:icon] forState:UIControlStateSelected];
    }
    
    item.icon = icon;
    
    return item;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self)
    {
        _separatorView = [[UIView alloc]init];
        _separatorView.backgroundColor = COLOR_SEPARATOR;
        [self addSubview:_separatorView];
        
        _sourceLabel = [[UILabel alloc]init];
        _sourceLabel.backgroundColor = [UIColor clearColor];
        _sourceLabel.font = FONT_HELVETICANEUE(11);
        _sourceLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_sourceLabel];
        
        _tempStatusBarView = [[UIView alloc] init];
        [self addSubview:_tempStatusBarView];
        
        _highlightLineView = [[UIView alloc] init];
        _highlightLineView.backgroundColor = COLOR_HIGHLIGHT_TITLE;
        [self addSubview:_highlightLineView];
        
        self.titleLabel.font = FONT_HELVETICANEUE(FONT_SIZE);
        
        self.backgroundColor = COLOR_LIGHT_BLACK;
        [self setTitleColor:COLOR_WHITE_NORMAL forState:UIControlStateNormal];
        _sourceLabel.textColor = [UIColor lightGrayColor];
    }
    
    return self;
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 6, 0);
    self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 8, 4);
    
    _highlightLineView.frame = RECT(0, self.height - _highlightHeight, self.width, _highlightHeight);
    _separatorView.frame = CGRectMake(self.width, 0, 1, self.height);
    _sourceLabel.frame = CGRectMake(0, self.height - 14 - _highlightHeight, self.width, 14);
    _tempStatusBarView.frame = RECT(0, -20, self.width, 20);
}

- (void)hideTempStatusBarView
{
    _tempStatusBarView.hidden = YES;
}

- (void)showTempStatusBarView
{
    _tempStatusBarView.hidden = NO;
}

- (void)setHighlighted:(BOOL)highlighted
{
    
}

- (void)setImage:(UIImage *)image forState:(UIControlState)state
{
    [super setImage:image forState:UIControlStateNormal];
    [super setImage:image forState:UIControlStateHighlighted];
    [super setImage:image forState:UIControlStateSelected];
}

- (void)setSelected:(BOOL)selected
{
    [super setSelected:selected];
    
    if (selected)
    {
        UIColor *color = COLOR_HIGHLIGHT_TITLE;
        _highlightLineView.hidden = NO;
        [self setTitleColor:color forState:UIControlStateNormal];
        [self setImage:[UIImage imageNamed:_icon color:color] forState:UIControlStateNormal];
    }
    else
    {
        _highlightLineView.hidden = YES;
        [self setTitleColor:COLOR_WHITE_NORMAL forState:UIControlStateNormal];
        [self setImage:[UIImage imageNamed:_icon] forState:UIControlStateNormal];
    }
    
    _tempStatusBarView.backgroundColor = self.backgroundColor;
}
@end
